package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentCheckPlanDetail;
import com.mxpio.erp.common.equipment.service.EquipmentCheckPlanDetailService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
* @Description: 点巡检计划
* @Author: jeecg-boot
* @Date:   2021-09-16
* @Version: V1.0
*/
@Tag(name = "EquipmentCheckPlanDetailController", description = "点巡检计划明细")
@RestController
@RequestMapping("/erp/equipment/spotcheck/tpmCheckPlanDetail")
@Slf4j
public class EquipmentCheckPlanDetailController {

   @Autowired
   private EquipmentCheckPlanDetailService equipmentCheckPlanDetailService;


   /**
    * 通过主表ID查询
    * @return
    */
   @GetMapping(value = "/page")
   @Operation(summary = "点检计划明细-通过主表ID查询", description = "点检计划明细-通过主表ID查询", method = "GET")
   public Result<?> listEquipmentCheckPlanDetailByMainId(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         Criteria criteria) {
       Pageable page = PageRequest.of(pageNo-1, pageSize);
       Page<EquipmentCheckPlanDetail> pageList = equipmentCheckPlanDetailService.listPage(EquipmentCheckPlanDetail.class,page, criteria);
       return Result.OK(pageList);
   }

   /**
    * 添加
    * @param equipmentCheckPlanDetail
    * @return
    */
   @PostMapping(value = "/add")
   @Operation(summary = "点检计划明细-添加", description = "点检计划明细-添加", method = "POST")
   public Result<?> addEquipmentCheckPlanDetail(@RequestBody EquipmentCheckPlanDetail equipmentCheckPlanDetail) {
       equipmentCheckPlanDetailService.addDetail(equipmentCheckPlanDetail);
       return Result.OK("添加成功！");
   }

   @PostMapping(value = "/add/{planId}/{categoryId}")
   @Operation(summary = "点检计划明细-按类别添加", description = "点检计划明细-按类别添加", method = "POST")
   public Result addEquipmentCheckPlanDetailByCategory(@PathVariable("planId") String planId,@PathVariable("categoryId")String categoryId) {
       equipmentCheckPlanDetailService.addDetailByCategory(planId,categoryId);
       return Result.OK("添加成功！");
   }

   /**
    * 编辑
    * @param equipmentCheckPlanDetail
    * @return
    */
   @PutMapping(value = "/edit")
   @Operation(summary = "点检计划明细-编辑", description = "点检计划明细-编辑", method = "PUT")
   public Result<?> editEquipmentCheckPlanDetail(@RequestBody EquipmentCheckPlanDetail equipmentCheckPlanDetail) {
       equipmentCheckPlanDetailService.update(equipmentCheckPlanDetail);
       return Result.OK("编辑成功!");
   }

   /**
    * 通过id删除
    * @param id
    * @return
    */
   @DeleteMapping(value = "/delete/{id}")
   @Operation(summary = "点检计划明细-通过id删除", description = "点检计划明细-通过id删除", method = "DELETE")
   public Result<?> deleteEquipmentCheckPlanDetail(@PathVariable(name="id",required=true) String id) {
       equipmentCheckPlanDetailService.remove(EquipmentCheckPlanDetail.class,id);
       return Result.OK("删除成功!");
   }

   /**
    * 批量删除
    * @param ids
    * @return
    */
   @DeleteMapping(value = "/deleteBatch")
   @Operation(summary = "点检计划明细-批量删除", description = "点检计划明细-批量删除", method = "DELETE")
   public Result<?> deleteBatchEquipmentCheckPlanDetail(@RequestParam(name="ids",required=true) String ids) {
       Criteria c = Criteria.create().addCriterion("id", Operator.IN, Arrays.asList(ids.split(",")));
       equipmentCheckPlanDetailService.removeBatch(EquipmentCheckPlanDetail.class,c);
       return Result.OK("批量删除成功!");
   }

//    /**
//     * 导出
//     * @return
//     */
//    @RequestMapping(value = "/exportEquipmentCheckPlanDetail")
//    public ModelAndView exportEquipmentCheckPlanDetail(HttpServletRequest request, EquipmentCheckPlanDetail equipmentCheckPlanDetail) {
//		 // Step.1 组装查询条件
//		 QueryWrapper<EquipmentCheckPlanDetail> queryWrapper = QueryGenerator.initQueryWrapper(equipmentCheckPlanDetail, request.getParameterMap());
//		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//
//		 // Step.2 获取导出数据
//		 List<EquipmentCheckPlanDetail> pageList = equipmentCheckPlanDetailService.list(queryWrapper);
//		 List<EquipmentCheckPlanDetail> exportList = null;
//
//		 // 过滤选中数据
//		 String selections = request.getParameter("selections");
//		 if (oConvertUtils.isNotEmpty(selections)) {
//			 List<String> selectionList = Arrays.asList(selections.split(","));
//			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
//		 } else {
//			 exportList = pageList;
//		 }
//
//		 // Step.3 AutoPoi 导出Excel
//		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
//		 mv.addObject(NormalExcelConstants.FILE_NAME, "点检计划明细"); //此处设置的filename无效 ,前端会重更新设置一下
//		 mv.addObject(NormalExcelConstants.CLASS, EquipmentCheckPlanDetail.class);
//		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("点检计划明细报表", "导出人:" + sysUser.getRealname(), "点检计划明细"));
//		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
//		 return mv;
//    }
//
//    /**
//     * 导入
//     * @return
//     */
//    @RequestMapping(value = "/importEquipmentCheckPlanDetail/{mainId}")
//    public Result<?> importEquipmentCheckPlanDetail(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
//		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
//		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
//			 MultipartFile file = entity.getValue();// 获取上传文件对象
//			 ImportParams params = new ImportParams();
//			 params.setTitleRows(2);
//			 params.setHeadRows(1);
//			 params.setNeedSave(true);
//			 try {
//				 List<EquipmentCheckPlanDetail> list = ExcelImportUtil.importExcel(file.getInputStream(), EquipmentCheckPlanDetail.class, params);
//				 for (EquipmentCheckPlanDetail temp : list) {
//                    temp.setPlanId(mainId);
//				 }
//				 long start = System.currentTimeMillis();
//				 equipmentCheckPlanDetailService.saveBatch(list);
//				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
//				 return Result.OK("文件导入成功！数据行数：" + list.size());
//			 } catch (Exception e) {
//				 log.error(e.getMessage(), e);
//				 return Result.error("文件导入失败:" + e.getMessage());
//			 } finally {
//				 try {
//					 file.getInputStream().close();
//				 } catch (IOException e) {
//					 e.printStackTrace();
//				 }
//			 }
//		 }
//		 return Result.error("文件导入失败！");
//    }


}
